//
//  StatusCellTopView.swift
//  WeiBo_One
//
//  Created by apple on 15/10/27.
//  Copyright © 2015年 蜡笔小炎. All rights reserved.
//

import UIKit
import SDWebImage//statusIconWidth

class StatusCellTopView: UIView {
    
    
    // 数据模型
    var statusViewModel: StatusViewModel? {
        didSet {
            //设置名字
            nameLabel.text = statusViewModel?.status.user?.screen_name!
            //设置头像
            iconView.sd_setImageWithURL(statusViewModel?.userProfileUrl, placeholderImage: statusViewModel?.userDefaultImage)
            //设置Vip认证
            vipIconView.image = statusViewModel?.userVipImage
            //会员图标
            memberIconView.image = statusViewModel?.userMemberImage
            
            let index = "Tue Oct 27 22:14:03".endIndex    //  Tue Oct 27 22:14:03 +0800 2015
            timeLabel.text = statusViewModel?.status.created_at?.substringToIndex(index)
            sourceLabel.text = "来自牛逼微博"
            
        }
    }
    
    // MARK: - 懒加载控件
    /// 用户头像
    private lazy var iconView: UIImageView = UIImageView(imageName: "avatar_default_big")
    /// 姓名标签
    private lazy var nameLabel: UILabel = UILabel(title: "蜡笔小炎")
    /// 会员图标
    private lazy var memberIconView: UIImageView = UIImageView(imageName: "common_icon_membership_level1")
    /// 认证图标
    private lazy var vipIconView: UIImageView = UIImageView(imageName: "avatar_grassroot")
    /// 时间标签
    private lazy var timeLabel: UILabel = UILabel(title: "刚刚", fontSize: 11, color: UIColor.orangeColor())
    /// 来源标签
    private lazy var sourceLabel: UILabel = UILabel(title: "黑马微博", fontSize: 11)

    //MARK: - 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")}

    }

//MARK: - 布置界面
extension StatusCellTopView {
    /// 设置界面
    private func setupUI() {
        // 0. 背景颜色
        backgroundColor = UIColor(white: 0.92, alpha: 1.0)
        // 设置头像图片
        
//        iconView.backgroundColor = UIColor.redColor()
        // 1. 添加控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberIconView)
        addSubview(vipIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        
        // 设置自动布局
        autoSet()
    }
    
    /// 自动布局
    private func autoSet() {
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(statusCellMargin)
            make.left.equalTo(self.snp_left).offset(statusCellMargin)
            make.width.equalTo(statusIconWidth)
            make.height.equalTo(statusIconWidth)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(statusCellMargin)
        }
        memberIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(statusCellMargin)
        }
        vipIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(iconView.snp_right).offset(statusCellMargin)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(statusCellMargin)
        }

    }
}
